using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using DataObject.Core;
using System.Xml.Serialization;
using DataObject.Database;

namespace DataObject.DataAccess
{
    public partial class ExchangeRateDAO : DataBaseProcessBase<ExchangeRateEntity, OBSDataContext>
    {
        public static ExchangeRateDAO CreateInstant()
        {
            return new ExchangeRateDAO();
        }

        public IList<ExchangeRate> GetList(System.DateTime rateDate)
        {
            IList<ExchangeRate> result = null;

            try
            {
                using (OBSDataContext db = new OBSDataContext(base.connectionString))
                {
                    var query = (from q in db.ExchangeRateEntities
                                 where q.RateDate.Date == rateDate.Date
                                 select q).SingleOrDefault();
                    if (query != null)
                        result.DeserializeObj(query.ExchangeTable);
                }
            }
            catch
            {

            }

            return result;
        }

        public bool WriteList(System.DateTime rateDate, IList<ExchangeRate> exchangeRates)
        {
            bool result = false;

            try
            {
                using (OBSDataContext db = new OBSDataContext(base.connectionString))
                {
                    var query = (from q in db.ExchangeRateEntities
                                 where q.RateDate.Date == rateDate.Date
                                 select q).SingleOrDefault();
                    if (query != null)
                    {
                        query.ExchangeTable = exchangeRates.SerializeObj();
                    }
                    else
                    {
                        db.ExchangeRateEntities.InsertOnSubmit(new ExchangeRateEntity
                        {
                            RateDate = rateDate,
                            ExchangeTable = exchangeRates.SerializeObj()
                        });

                        // Insert the new exchange rate table with name EXCHANGERATE2
                        IList<string> currencies = new List<string>();

                        foreach (var item in Enum.GetValues(typeof(CurrencyEnum)))
                        {
                            currencies.Add(((CurrencyEnum)item).GetStringValue());
                        }

                        foreach (var item in exchangeRates)
                        {
                            if (item.Code == "VND") continue;
                            db.ExchangeRate2s.InsertOnSubmit(new ExchangeRate2
                            {
                                ExchangeDate = rateDate,
                                CurrencyID = currencies.IndexOf(item.Code),
                                CurrencyCode = item.Code,
                                Transfer = item.UnitPrice
                            });
                        }

                    }
                    db.SubmitChanges();
                }

                result = true;
            }
            catch
            {

            }

            return result;
        }
    }
}
